package com.aerotechnic.dao.implementation;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aerotechnic.dao.interfaces.MecanicienDaoInc;
import com.aerotechnic.entities.JalonBiblio;
import com.aerotechnic.entities.MacroTache;
import com.aerotechnic.entities.Mecanicien;

@Repository("MecanicienDao")
public class MecanicienDao implements MecanicienDaoInc {

	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void save(Mecanicien t) {

		// TODO Auto-generated method stub

		getCurrentSession().save(t);

	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub

		Mecanicien e = findByID(id);
		if (e != null)
			getCurrentSession().delete(e);

	}

	@Override
	public Mecanicien findByID(int id) {
		// TODO Auto-generated method stub

		Session session = sessionFactory.getCurrentSession();

		return (Mecanicien) session.createQuery("from Mecanicien where id= " + id)
				.list().get(0);
	}

	@Override
	public void update(Mecanicien t) {
		// TODO Auto-generated method stub

		getCurrentSession().update(t);
	}

	@SuppressWarnings("unchecked")
	public List<Mecanicien> findAll() {
		// TODO Auto-generated method stub

		return getCurrentSession().createQuery("from Mecanicien").list();
	}
	
	
	
	@Override
	public int countAllMecanicien() {
		// TODO Auto-generated method stub
		List allMecanicien = getCurrentSession().createQuery("from Mecanicien ")
				.list();
		return allMecanicien==null?0:allMecanicien.size();
	}

	@Override
	public List<Mecanicien> find(int startIndex, int pageSize) {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from Mecanicien")
				.setFirstResult(startIndex).setMaxResults(pageSize).list();   
		
	}

}
